package tech.$2306.comment.controller;

import org.springframework.web.bind.annotation.*;
import tech.$2306.comment.model.vo.MessageVO;
import tech.$2306.comment.model.vo.Response;
import tech.$2306.comment.model.vo.input.MessageInput;
import tech.$2306.comment.service.MessageService;
import tech.$2306.comment.util.IPAddressUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author Wangls
 * @since 2021-04-10 11:10
 */
@RestController
@RequestMapping("/api/message")
public class MessageController {

    @Resource
    private IPAddressUtil ipAddressUtil;

    @Resource
    private MessageService messageService;

    @PostMapping("/")
    public Response<?> saveMessage(@RequestBody MessageInput messageInput, HttpServletRequest req) {
        String ipAddress = ipAddressUtil.getIPAddress(req);
        messageInput.setMessagePostIp(ipAddress);
        if (messageService.saveMessage(messageInput)) {
            return Response.getNormalNotifyMessage();
        }
        return Response.getWarnNotifyMessage();
    }

    @GetMapping("/")
    public Response<?> getMessageList() {
        List<MessageVO> messageList = messageService.getMessageList();
        return Response.getSuccessRespWithData(messageList);
    }

}
